<pre class="code">
<span class="srcline"><span class="lineno"><a href="42,1" id="srcline1"> 1</a></span><span class="line"></span></span>
<span class="srcline"><span class="lineno"><a href="42,2" id="srcline2"> 2</a></span><span class="line"></span></span>
<span class="srcline"><span class="lineno"><a href="42,3" id="srcline3"> 3</a></span><span class="line"><span class="comment">% This function updates mesh-specific attributes of a body B</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,4" id="srcline4"> 4</a></span><span class="line"></span></span>
<span class="srcline"><span class="lineno"><a href="42,5" id="srcline5"> 5</a></span><span class="line"><span class="keyword">function</span> <span class="var type1" id="S2T34U3">B</span> = body_updateMesh( <span class="mxinfo" id="T34:U2"><span class="mxinfo" id="T34:U3"><span class="var type1" id="S2T34U6">B</span></span></span> )</span></span>
<span class="srcline"><span class="lineno"><a href="42,6" id="srcline6"> 6</a></span><span class="line"> <span class="comment">%#codegen</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,7" id="srcline7"> 7</a></span><span class="line"></span></span>
<span class="srcline"><span class="lineno"><a href="42,8" id="srcline8"> 8</a></span><span class="line">    <span class="comment">% Update world coordinates</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,9" id="srcline9"> 9</a></span><span class="line">    <span class="keyword">for</span> <span class="var type1" id="S3T1U9">v</span> = <span class="mxinfo" id="T1:U6">1</span>:<span class="mxinfo" id="T1:U7"><span class="var type1" id="S2T34U13">B</span>.num_verts</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,10" id="srcline10">10</a></span><span class="line">        <span class="comment">%B.verts_world(v,:) = B.u + qtrotate( B.quat, B.verts_local(v,:)' );</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,11" id="srcline11">11</a></span><span class="line">    <span class="keyword">end</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,12" id="srcline12">12</a></span><span class="line"></span></span>
<span class="srcline"><span class="lineno"><a href="42,13" id="srcline13">13</a></span><span class="line">    <span class="comment">% Update face normals        TODO: normals and tvecs should be calculated only when needed</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,14" id="srcline14">14</a></span><span class="line"><span class="comment">%     for f = 1:B.num_faces</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,15" id="srcline15">15</a></span><span class="line"><span class="comment">%         v1 = B.verts_world( B.faces(f,1),: );</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,16" id="srcline16">16</a></span><span class="line"><span class="comment">%         v2 = B.verts_world( B.faces(f,2),: );</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,17" id="srcline17">17</a></span><span class="line"><span class="comment">%         v3 = B.verts_world( B.faces(f,3),: );</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,18" id="srcline18">18</a></span><span class="line"><span class="comment">%         n = cross3(v3-v2,v1-v2); </span></span></span>
<span class="srcline"><span class="lineno"><a href="42,19" id="srcline19">19</a></span><span class="line"><span class="comment">%         B.face_norms(f,:) = n / norm(n);</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,20" id="srcline20">20</a></span><span class="line"><span class="comment">%     end</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,21" id="srcline21">21</a></span><span class="line"><span class="comment">% </span></span></span>
<span class="srcline"><span class="lineno"><a href="42,22" id="srcline22">22</a></span><span class="line"><span class="comment">%     % Update t vectors </span></span></span>
<span class="srcline"><span class="lineno"><a href="42,23" id="srcline23">23</a></span><span class="line"><span class="comment">%     for e =1:B.num_edges</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,24" id="srcline24">24</a></span><span class="line"><span class="comment">%         E = B.verts_world(B.edges(e,2),:) - B.verts_world(B.edges(e,1),:);  </span></span></span>
<span class="srcline"><span class="lineno"><a href="42,25" id="srcline25">25</a></span><span class="line"><span class="comment">%         if B.edges(e,3)</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,26" id="srcline26">26</a></span><span class="line"><span class="comment">%             B.tvecs(e,1:3) = cross3(B.face_norms(B.edges(e,3),:), E);  % t1</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,27" id="srcline27">27</a></span><span class="line"><span class="comment">%             B.tvecs(e,1:3) = B.tvecs(e,1:3) / norm(B.tvecs(e,1:3)); % This may not be strictly necessary, but let's normalize the t vecs</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,28" id="srcline28">28</a></span><span class="line"><span class="comment">%         end</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,29" id="srcline29">29</a></span><span class="line"><span class="comment">%         if B.edges(e,4)</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,30" id="srcline30">30</a></span><span class="line"><span class="comment">%             B.tvecs(e,4:6) = cross3(E, B.face_norms(B.edges(e,4),:));  % t2</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,31" id="srcline31">31</a></span><span class="line"><span class="comment">%             B.tvecs(e,4:6) = B.tvecs(e,4:6) / norm(B.tvecs(e,4:6)); </span></span></span>
<span class="srcline"><span class="lineno"><a href="42,32" id="srcline32">32</a></span><span class="line"><span class="comment">%         end</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,33" id="srcline33">33</a></span><span class="line"><span class="comment">%     end</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,34" id="srcline34">34</a></span><span class="line"><span class="comment">%     </span></span></span>
<span class="srcline"><span class="lineno"><a href="42,35" id="srcline35">35</a></span><span class="line"><span class="comment">%     % Update bounding box </span></span></span>
<span class="srcline"><span class="lineno"><a href="42,36" id="srcline36">36</a></span><span class="line"><span class="comment">%     maxBBexpand = 0.05;      % TODO: replace hard-coded epsilons</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,37" id="srcline37">37</a></span><span class="line"><span class="comment">%     </span></span></span>
<span class="srcline"><span class="lineno"><a href="42,38" id="srcline38">38</a></span><span class="line"><span class="comment">%     AABB_min = min(B.verts_world);</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,39" id="srcline39">39</a></span><span class="line"><span class="comment">%     AABB_max = max(B.verts_world); </span></span></span>
<span class="srcline"><span class="lineno"><a href="42,40" id="srcline40">40</a></span><span class="line"><span class="comment">%     vec = AABB_max-AABB_min;</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,41" id="srcline41">41</a></span><span class="line"><span class="comment">%     length = norm(vec);</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,42" id="srcline42">42</a></span><span class="line"><span class="comment">%     expansion = min(maxBBexpand, 0.15*length);   % Increase bounding box by 3% or maxBBexpand along diagonal.</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,43" id="srcline43">43</a></span><span class="line"><span class="comment">%     B.AABB_min = AABB_min - expansion*vec;     </span></span></span>
<span class="srcline"><span class="lineno"><a href="42,44" id="srcline44">44</a></span><span class="line"><span class="comment">%     B.AABB_max = AABB_max + expansion*vec;    </span></span></span>
<span class="srcline"><span class="lineno"><a href="42,45" id="srcline45">45</a></span><span class="line">    </span></span>
<span class="srcline"><span class="lineno"><a href="42,46" id="srcline46">46</a></span><span class="line"><span class="keyword">end</span></span></span>
<span class="srcline"><span class="lineno"><a href="42,47" id="srcline47">47</a></span><span class="line"></span></span>
</pre>
